//
//  HomerViewController.m
//  XiJianChui
//
//  Created by aaa on 16/7/18.
//  Copyright © 2016年 FanShuHan. All rights reserved.
//

#import "HomerViewController.h"
#import "HomeHeaderView.h"
#import "HomeTableViewCell.h"
#import "HomeHairstyleVC.h"
#import "HomeSettingVC.h"
#import "HomeCellModel.h"

static NSString *cellID = @"tableViewCell";
@interface HomerViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak) UITableView *tableView;
//@property(nonatomic,strong) NSMutableArray *cellTitles;
//@property(nonatomic,strong) NSMutableArray *classNames;
@property(nonatomic,strong) NSArray *modelArr;

@end

@implementation HomerViewController

//懒加载
//- (NSMutableArray *)classNames {
//    if (_classNames == nil) {
//        _classNames = [NSMutableArray array];
//    }
//    return _classNames;
//}

//- (NSMutableArray *)cellTitles {
//    if (_cellTitles == nil) {
//        _cellTitles = [NSMutableArray array];
//    }
//    return _cellTitles;
//}

- (NSArray *)modelArr {
    if (_modelArr == nil) {
        NSArray *modelArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"HomeModel.plist" ofType:nil]];
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dict in modelArr) {
            HomeCellModel *model = [HomeCellModel cellWithDict:dict];
            [arr addObject:model];
        }
        _modelArr = arr;
    }
    
    return _modelArr;
}

//给数组赋值方法
//- (void)addCellTitles:(NSString *)cellTitles classNames:(NSString *)classNames {
//    [self.cellTitles addObject:cellTitles];
//    [self.classNames addObject:classNames];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"个人中心";

    [self setupHeaderView];
    [self setupTableView];
    
//    [self addCellTitles:@"我的发型" classNames:@"HomeHairstyleVC"];
//    [self addCellTitles:@"个人设置" classNames:@"HomeSettingVC"];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeTableViewCell" bundle:nil] forCellReuseIdentifier:cellID];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

//头部view
- (void)setupHeaderView {
    HomeHeaderView *headerView = [HomeHeaderView headerView];
    headerView.frame = CGRectMake(0, 64, QhScreenW, 100);
    
    [self.view addSubview:headerView];
}

//tableView
- (void)setupTableView {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 164, QhScreenW, QhScreenH) style:UITableViewStyleGrouped];
    tableView.scrollEnabled = NO;
    tableView.dataSource = self;
    tableView.delegate = self;
    self.tableView = tableView;
    
    [self.view addSubview:tableView];
}

#pragma mark - UITableViewDelegate&&UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return 3;
    }
    return 2;
}

int tag = 0;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HomeTableViewCell alloc] init];
    }
//    cell.textLabel.text = [NSString stringWithFormat:@"%ld,%ld",indexPath.section,indexPath.row];
//    cell = (HomeTableViewCell *)cell;
    HomeTableViewCell *homeCell = (HomeTableViewCell *)cell;
    homeCell.model = self.modelArr[tag];
    if (tag < 8) {
        tag += 1;
    }else {
        tag = 0;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"%ld",indexPath.row);
//    NSString *className = self.classNames[indexPath.row];
//    Class class = NSClassFromString(className);
//    if (class) {
//        UIViewController *vc = class.new;
//        vc.title = self.cellTitles[indexPath.row];
//        [self.navigationController pushViewController:vc animated:YES];
//        }
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HomeSettingVC *vc = [[HomeSettingVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
